package me.zk.practice1.s;

/**
 * @author zhangkai
 * @date 2023/7/28 11:13
 */
public class LS5 {
    //暴力解答
    public String longestPalindrome(String s) {
        if (s.length() < 2) {
            return s;
        }

        int maxLen = 1;
        int begin = 0;
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length -1; i++) {
            for (int j = 1; j < chars.length; j++) {
                if (j - i + 1 > maxLen && validPalindrome(chars,i,j)) {
                    maxLen = j-i+1;
                    begin = i;
                }
            }
        }
        return s.substring(begin,begin+maxLen);

    }
    //判断是否是回文
    private boolean validPalindrome(char[] chars,int begin, int end) {
       while (begin < end) {
           if (chars[begin] != chars[end]) {
               return false;
           }
           begin++;
           end--;

       }

       return true;
    }

}
